package tech.runchen.gz5055.oms.modules.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tech.runchen.gz5055.oms.common.annotation.RequiresPermissions;
import tech.runchen.gz5055.oms.common.annotation.SysLog;
import tech.runchen.gz5055.oms.common.base.AbstractController;
import tech.runchen.gz5055.oms.common.enums.BusinessType;
import tech.runchen.gz5055.oms.common.enums.UserType;
import tech.runchen.gz5055.oms.common.utils.PageInfo;
import tech.runchen.gz5055.oms.common.utils.Result;
import tech.runchen.gz5055.oms.modules.system.entity.AddressEntity;
import tech.runchen.gz5055.oms.modules.system.service.IAddressService;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 地址信息表 前端控制器
 * </p>
 *
 * @author zzj
 * @since 2023-10-10
 */
@Api(tags = "管理平台 - 地址管理")
@RestController
@RequestMapping("/sys/address")
public class SysAddressController extends AbstractController {

    @Autowired
    private IAddressService addressService;

    @ApiOperation("收货地址列表（分页")
    @RequiresPermissions(value = {UserType.SPD, UserType.NURSE})
    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        PageInfo page = addressService.queryPage(params);
        return Result.success(page);
    }

    @ApiOperation("收货地址列表（不分页")
    @RequiresPermissions(value = {UserType.NURSE, UserType.SPD})
    @GetMapping("/select")
    public Result select(@RequestParam Map<String, Object> params) {
        List<AddressEntity> list = addressService.querySelect(params);
        return Result.success(list);
    }

    @ApiOperation("收货地址详情")
    @RequiresPermissions(value = {UserType.NURSE, UserType.SPD})
    @GetMapping("/{addressId}")
    public Result info(@PathVariable("addressId") Long addressId) {
        AddressEntity address = addressService.getById(addressId);
        return Result.success(address);
    }

    @SysLog(title = "保存用户信息", businessType = BusinessType.INSERT)
    @ApiOperation("保存收货地址信息")
    @RequiresPermissions()
    @PostMapping("/save")
    public Result save(@Validated @RequestBody AddressEntity address) {
        addressService.saveAddress(address);
        return Result.success();
    }

    @SysLog(title = "更新收货地址", businessType = BusinessType.UPDATE)
    @ApiOperation("更新收货地址信息")
    @RequiresPermissions()
    @PostMapping("/update")
    public Result update(@Validated @RequestBody AddressEntity address) {
        addressService.updateAddress(address);
        return Result.success();
    }

    @ApiOperation("删除收货地址信息")
    @RequiresPermissions()
    @DeleteMapping("/delete/{addressId}")
    public Result delete(@PathVariable("addressId") long addressId) {
        addressService.deleteAddress(addressId);
        return Result.success();
    }
}
